package com.kaixuan.mvvmdemo;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.kaixuan.mvvmdemo.bean.UserBean;
import com.kaixuan.mvvmdemo.databinding.ActivityMainBinding;


public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;
    UserBean userBean = new UserBean("小明", 20);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 两种方式获取binding对象
        //      一、在Activity中直接绑定
                binding = DataBindingUtil.setContentView(this,R.layout.activity_main);
        //      二、针对某个view绑定
//        View view = getLayoutInflater().inflate(R.layout.activity_main, null);
//        setContentView(view);
//        binding = DataBindingUtil.bind(view);
        // 设置布局中声明的变量
        binding.setUser(userBean);
        binding.setActivity(this);

        binding.tvUser.setText("用户信息");
    }

    // 布局中响应按钮点击的事件
    public void onEditClick(View view) {
        userBean.name.set("小强");
        userBean.age.set(22);
        // 修改数据后，无需更新界面，内容自动反映到界面上
    }
    // 进入登录界面
    public void onLoginClick(View view) {
        startActivity(new Intent(this,LoginActivity.class));
    }
}
